package com.briup.ch02;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.*;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/11/6
 * @Description:
 */
public class Test10000 {
    private String driverClass="com.mysql.cj.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/briup";
    private String username="briup";
    private String password="briup";
    private long startTime;
    private long endTime;
    @Before
    public void start(){
        startTime=System.currentTimeMillis();
        System.out.println("开始执行"+startTime);
    }
    @After
    public void end() {
        endTime = System.currentTimeMillis();
        System.out.println("结束执行"+endTime);
        System.out.println("总执行时间："+(endTime-startTime)+"ms");
    }
    @Test
    public void testNomal(){
        //Statement
        Connection conn=null;
        Statement stmt=null;
        try{
            Class.forName(driverClass);
            conn= DriverManager.getConnection(url,username,password);
            stmt=conn.createStatement();
            for(int i=0;i<10000;i++){
                stmt.execute("insert into user(name,password) values('rose"+i+"',20)");
            }

        }catch(ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testSpecial(){
        //PreparedStatement+Batch+Transaction
        Connection conn=null;
        PreparedStatement pstmt=null;
        try{
            Class.forName(driverClass);
            this.url+="?useServerPrepStmts=true";
            conn= DriverManager.getConnection(url,username,password);
            conn.setAutoCommit(false);
            pstmt=conn.prepareStatement("insert into user(name,password) values(?,20)");
            for(int i=0;i<10000;i++){
                //设置值
                pstmt.setString(1,"jack"+i);
                //添加到批处理中
                pstmt.addBatch();
                //到达300条后执行一次
                if(i%300==0){
                    pstmt.executeBatch();
                }
            }
            //在最后执行批处理（清理池）
            pstmt.executeBatch();
            //提交事务
            conn.commit();
            System.out.println("pstm+batch+trans"+"执行成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            //回滚事务
            try{
                conn.rollback();
                System.out.println("pstm+batch+trans"+"执行失败，已回滚");
            }catch (SQLException e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            //关闭资源
            try{
              if(pstmt!=null)
                  pstmt.close();
              if(conn!=null)
                  conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
